package com.main.crossdomaineventmanagement.event.config;

import com.main.crossdomaineventmanagement.event.core.TransactionListenerImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.message.MessageQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Set;

@Slf4j
@Configuration
public class RocketMQConfig {

    @Value("${rocketmq.name-server:localhost:9876}")
    private String nameServer;

    // 事务生产者
    @Bean(name="transactionProducer", initMethod = "start", destroyMethod = "shutdown")
    public TransactionMQProducer transactionProducer() throws MQClientException {
        TransactionMQProducer producer = new TransactionMQProducer("TransactionProducerGroup");
        producer.setNamesrvAddr(nameServer);
        producer.setTransactionListener(new TransactionListenerImpl());
        return producer;
    }

    // 普通事件生产者
    @Bean(name="defaultProducer", initMethod = "start", destroyMethod = "shutdown")
    public DefaultMQProducer defaultProducer() throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer("DefaultProducerGroup");
        producer.setNamesrvAddr(nameServer);
        producer.setRetryTimesWhenSendFailed(3);
        return producer;
    }

    // 回滚专用生产者
    @Bean(name="rollbackProducer", initMethod = "start", destroyMethod = "shutdown")
    public TransactionMQProducer rollbackProducer() throws MQClientException {
        TransactionMQProducer producer = new TransactionMQProducer("RollbackProducerGroup");
        producer.setNamesrvAddr(nameServer);
        return producer;
    }

}